using System;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using NetProtocol.Client;
using NetProtocol.Enum;
using NetProtocol.POD;
using Sirenix.OdinInspector.Editor.Modules;
using static NetProtocol.Client.CNetCenterFriend;
using static NetProtocol.Client.CNetItem;
using static UnityEngine.Rendering.DebugUI;

namespace IQIGame.Onigao.GamePlay
{
    public class FriendModule : Singleton<FriendModule>, CNetCenterFriend
    {
        private FriendModule() { }

        private FriendModuleData friendModuleData => GameDataCenter.PlayerData.friend;
        public void InitModule(PlayerPOD playerPOD)
        {

            GameDataCenter.PlayerData.friend = new FriendModuleData();

            foreach (var f in playerPOD.friends)
            {

                UpdateData(f);

            }
        }


        public void ClearModule()
        {
            GameDataCenter.PlayerData.friend?.Clear();
        }
        private void UpdateData(FriendPOD friendData)
        {//更新和添加
            FriendData data = FriendData.NewFromPool().Sync(friendData);
            switch (friendData.type)
            {
                case FriendType.FRIEND:
                    {
                        this.friendModuleData.dicCurrFriends[data.id] = data;
                        break;
                    }
                case FriendType.APPLY:
                    {
                        this.friendModuleData.dicCurrApply[data.id] = data;
                        break;
                    }
                case FriendType.TEMP:
                    {
                        break;
                    }
                case FriendType.BLACKLIST:
                    {
                        this.friendModuleData.dicCurrBlack[data.id] = data;
                        break;
                    }
                case FriendType.MY_APPLY:
                    {
                        this.friendModuleData.dicCurrMyApply[data.id] = data;
                        break;
                    }
            }
        }

        public bool IsBlocked(long pid)
        {
            var dic = friendModuleData.dicCurrBlack;
            foreach (var item in dic)
            {
                if (item.Value.pid == pid) return true;
            }
            return false;
        }
        public void RemoveSearchDic(FriendData friend)
        {
            friendModuleData.dicSearchRes.Remove(friend.id);
            MsgDispatcher.Broadcast(MsgEventType.SearchChanged);
        }
        #region Net层
        public static CNetCenterFriend NetCore => Instance;
        ISession CNetCenterFriend.GetSession()
        {
            return GameServerModule.Instance.CurrSession;
        }

        void CNetCenterFriend.SC_notifyAddFriend(ISession session, List<FriendPOD> friends)
        {
            for (int i = 0; i < friends.Count; i++)
            {
                var friend = friends[i];
                this.UpdateData(friend);
            }
            MsgDispatcher.Broadcast(MsgEventType.FriendChanged);
        }



        void CNetCenterFriend.SC_notifyDeleteFriend(ISession session, List<long> ids)
        {
            for (int i = 0; i < ids.Count; i++)
            {
                foreach (var dic in this.friendModuleData.dictionaries)
                {
                    if (dic.Remove(ids[i], out var friendData))
                    {
                        friendData.Put2Pool();
                        break;
                    }
                }

            }
            MsgDispatcher.Broadcast(MsgEventType.FriendChanged);
        }



        void CNetCenterFriend.SC_notifyUpdateFriend(ISession session, List<FriendPOD> friends)
        {

            for (int i = 0; i < friends.Count; i++)
            {
                var friend = friends[i];
                this.UpdateData(friend);
            }
            MsgDispatcher.Broadcast(MsgEventType.FriendChanged);
        }


        #endregion
        internal async UniTaskVoid SearchPlayer(string id)
        {
            var result = await NetCore.CS_searchPlayer(id);
            if (result.IsFail) return;
            UpSearchListData(result.friend);
            MsgDispatcher.Broadcast(MsgEventType.SearchChanged);
        }
        internal async UniTaskVoid RecommandPlayer()
        {
            var result = await NetCore.CS_recommend();
            UpSearchListData(result.friends);
            MsgDispatcher.Broadcast(MsgEventType.SearchChanged);
        }
        internal void DeleteSearchListData(long id = 0)
        {
            if (id == 0)
            {
                // 将所有项放入对象池
                foreach (var item in this.friendModuleData.dicSearchRes.Values)
                {
                    item.Put2Pool();
                }
                this.friendModuleData.dicSearchRes.Clear();

            }
            else
            {
                this.friendModuleData.dicSearchRes.Remove(id, out var item);
                item?.Put2Pool();
            }
            MsgDispatcher.Broadcast(MsgEventType.SearchChanged);
        }
        internal void UpSearchListData(FriendPOD friend)
        {
            Dictionary<long, FriendData> dic = new Dictionary<long, FriendData>();
            dic[friend.pid] = FriendData.NewFromPool().Sync(friend);
            this.friendModuleData.dicSearchRes = dic;
        }
        internal void UpSearchListData(List<FriendPOD> friends)
        {
            Dictionary<long, FriendData> dic = new Dictionary<long, FriendData>();
            foreach (var friend in friends)
            {
                dic[friend.pid] = FriendData.NewFromPool().Sync(friend);
            }
            this.friendModuleData.dicSearchRes = dic;
        }
        internal Dictionary<long, FriendData> GetSearchListData()
        {
            return friendModuleData.dicSearchRes;
        }

        internal Dictionary<long, FriendData> GetFriendListData()
        {
            return friendModuleData.dicCurrFriends;
        }

        internal Dictionary<long, FriendData> GetFriendApplyListData()
        {
            return friendModuleData.dicCurrApply;
        }
        internal Dictionary<long, FriendData> GetShieldListData()
        {
            return friendModuleData.dicCurrBlack;
        }


    }
}
